const express = require("express");
const router = express.Router();
const { conMysql } = require("../db");

//修改系统密码

//通过jwt验证token,获取用户信息,修改密码使用md5加密


// 在路由之前配置解析token的中间件
const { expressjwt: jwt } = require("express-jwt");
const config = require("../config");
//通过jwt解析token


router.post("/updateSystempassword", (req, res) => {


    console.log(req.body)
    const { oldpassword, newpassword } = req.body;

    //获取token
    const token = req.headers.authorization.split(" ").pop();
    //判断是否为空
    if (!oldpassword || !newpassword) {
        res.output("新密码或旧不能为空", 1001, null);
        return;
    }

    const { username, id } = req.auth;

    //判断旧密码是否正确
    const sql1 = `select * from db_user where id='${id}' and username='${username}' and password='${oldpassword}'`;

    conMysql(sql1).then((result) => {
        if (result.length == 0) {
            res.output("旧密码错误", 1001, null);
            return;
        }
        //修改密码
        const sql = `update db_user set password='${newpassword}' where username='${username}'`;
        conMysql(sql)
            .then((result) => {
            }).catch((err) => {
                res.output("修改失败", 500, null);
            });
        res.output("修改成功", 0, null);

    });

})

module.exports = router;